package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cssl.jianting.MyListener;
import com.cssl.pojo.User;
import com.cssl.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.RedisUtil;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    RedisUtil redisUtil;

    private ServletContext apption = MyListener.getApplication();

    /**
     * 登录
     * @param name 用户名
     * @param pwd 密码
     * @return 成功或失败
     */
    @RequestMapping("/login")
    public boolean login(String name, String pwd, HttpSession session){
        if (userService.count(new QueryWrapper<User>().eq("name", name)
                .and(userQueryWrapper -> userQueryWrapper.eq("pwd", pwd))) > 0) {
            session.setAttribute("user", name);
            List<String> list = (List<String>) apption.getAttribute("users");
            if (list.size() != 0 || list != null) {
                if (list.contains(name)) {
                    return false;
                }
            }
            list.add(name);
            apption.setAttribute("users", list);
            redisUtil.setStr("name",name);
            return true;
        }
        return false;
    }

    /**
     * 注册
     * @param user 注册信息
     * @return 成功或失败
     */
    @PostMapping("/register")
    public boolean register(User user){
        if(userService.count(new QueryWrapper<User>().eq("name",user.getName()))>0){
            //已存在
            return false;
        }
        return userService.save(user);
    }

    /**
     * 查询除管理员外的读者信息
     * @return ..
     */
    @PostMapping("/userAll")
    public PageInfo<User> userAll(Integer pageNum,Integer pageSize){
        //return userService.list(new QueryWrapper<User>().eq("type",0));
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<User> pageInfo = new PageInfo<>(userService.list(new QueryWrapper<User>().eq("type",0)));
        System.out.println(pageInfo.getPages());
        System.out.println(pageInfo.getList());
        return pageInfo;
    }

    /**
     * 通过id查询user
     * @param rid
     * @return
     */
    @PostMapping("/userById")
    public User userById(Integer rid){
        return userService.getById(rid);
    }

    /**
     * 编辑
     * @param user
     * @return
     */
    @PostMapping("/updUser")
    public boolean updUser(User user){
            return userService.update(user,new QueryWrapper<User>().eq("rid",user.getRid()));
    }

    /**
     * 删除
     * @param rid
     * @return
     */
    @PostMapping("/delUser")
    public boolean delUser(Integer rid){
        return userService.remove(new QueryWrapper<User>().eq("rid",rid));
    }

    @RequestMapping("/selectUser")
    public User selectUser(String uname){
       User us =  userService.getOne(new QueryWrapper<User>().eq("name",uname));
       return us;
    }

    @PostMapping("/idAll")
    public List<Integer> idAll(){
        List<Integer> listA = new ArrayList<>();
        List<User> list = userService.list(new QueryWrapper<User>().select("rid").eq("type",0));
        list.forEach(l->{
            listA.add(l.getRid());
        });
        return listA;
    }

    @PostMapping("/getValue")
    public String getValue(String key){
        return redisUtil.getStr(key);
    }

    @PostMapping("/uidall")
    public List<User> uidall(){
        List<User> lists= userService.getBaseMapper().selectList(new QueryWrapper<User>().eq("type",0));
        return lists;
    }
}

